<template>
  <div class="dialog-container">
    <el-dialog :visible.sync="showAdd" width="80%" top="1%" :title="title" :before-close="handleClose">
      <el-form ref="settingEntity" :model="settingEntity">
        <div class="modal-content">
          <table class="table-container">
            <tbody>
              <tr>
                <td class="td-label-2">
                  <must/>作者</td>
                <td>
                  <el-input v-model="settingEntity.author" name="作者" v-validate="'required'" placeholder="请输入作者" />
                  <span v-show="errors.has('作者')" style="color: red;">{{ errors.first('作者') }}</span>
                </td>
                <td class="td-label-2">
                  <must/>邮箱</td>
                <td>
                  <el-input v-model="settingEntity.email" name="邮箱" v-validate="'required'" placeholder="请输入邮箱" />
                  <span v-show="errors.has('邮箱')" style="color: red;">{{ errors.first('邮箱') }}</span>
                </td>
              </tr>
              <tr>
                <td class="td-label-2">
                  <must/>父包名</td>
                <td>
                  <el-input v-model="settingEntity.packageNm" name="父包名" v-validate="'required'" placeholder="请输入后端包名" />
                  <span v-show="errors.has('父包名')" style="color: red;">{{ errors.first('父包名') }}</span>
                </td>
                <td class="td-label-2">
                  <must/>模块名</td>
                <td>
                  <el-input v-model="settingEntity.mainModule" name="模块名" v-validate="'required'" placeholder="请输入前端模块前缀" />
                  <span v-show="errors.has('模块名')" style="color: red;">{{ errors.first('模块名') }}</span>
                </td>
              </tr>
              <tr>
                <td class="td-label-2">表前缀</td>
                <td>
                  <el-input v-model="settingEntity.tablePrefix" placeholder="表前缀" />
                </td>
              </tr>

            </tbody>
          </table>
        </div>
      </el-form>
      <div slot="footer">
        <el-button @click="handleClose" type="danger">关 闭</el-button>
        <el-button @click="handleSave" type="primary" :disabled="btnDisabled">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    downloadFile2Post
  } from '@/api/download';
  
  import {
    setSetting
  } from '@/api/generator/codeGeneratorPlus';

  /* 工具 */
  import * as common from '@/utils/base/common';

  export default {
    /*  内容: 配置 */
    name: 'setting',
    data() {
      return {
        restPath: 'generatorplus/codeGeneratorPlus',
        title: '自定义配置',
        btnDisabled: false, //按钮禁用状态
        settingEntity: {
          selectTables: '',
          author: 'superC',
          email: 'superc3707@gmail.com',
          packageNm: 'com.kim',
          mainModule: '',
          tablePrefix: '',
        },
        
      }
    },
    created() {
      this.initData()
    },
    props: ['showAdd', 'multipleSelection'],
    methods: {
      
      initData() {
        this.settingEntity.selectTables = this.multipleSelection
      },
      
      /**
       * 点击保存按钮
       */
      handleSave() {
        this.$validator.validateAll().then((result) => {
          if (result) {
            
            setSetting(this.settingEntity).then(response => {
            })
            
            // TODO 暂改为请求后不返回内容。
            this.handleClose()
            common.message(this, 'success', 2, '生成代码成功')
          }else{
            // 提示信息，并且防止连续多次请求
            common.tips(this, '请查看页面信息是否填写完整或正确！')
          }
        })
      },
      
      /**
       * 点击保存按钮  
       */
      handleSave2() {
        this.$validator.validateAll().then((result) => {
          if (result) {
            
            setSetting(this.settingEntity).then(response => {
              console.error(response)
              if(response.status === 200){
                this.handleClose()
              } else {
                common.message(this, 'success', 2, response.message)
              }
            }).catch(err => {
              common.message(this, 'error', 2, '生成代码失败!')
              console.error(err)
            })
          }else{
            // 提示信息，并且防止连续多次请求
            common.tips(this, '请查看页面信息是否填写完整或正确！')
          }
        })
      },
      
      handleClose() {
        this.$emit('closeDialog') // 向父组件发送关闭事件
      }
      
    } // methods end
  }
</script>
<style scoped>
  @import "~@/styles/form.css";
</style>